package market

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/market"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/market"
	"git.myscrm.cn/golang/stark/v4"
	"strconv"
)

type outboundTaskRepository struct {
}

// NewOutboundTaskRepository 客户池
func NewOutboundTaskRepository() OutboundTaskRepositoryIface {
	return &outboundTaskRepository{}
}

func (r *outboundTaskRepository) ImportCrowd(ctx context.Context, request *dto.TaskImportCrowdRequest) error {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "ImportCrowd request:%+v,GetClientConn err: %+v", request, err)
		return err
	}
	defer func() {
		_ = conn.Close()
	}()

	fileId, _ := strconv.ParseInt(request.FileId, 10, 64)

	client := pb.NewOutboundTaskServiceClient(conn)
	_, err = client.ImportCrowd(ctx, &pb.ImportCrowdRequest{
		TaskId:    request.TaskId,
		CrowdId:   request.CrowdId,
		FileId:    fileId,
		CompanyId: request.CompanyId,
	})
	if err != nil {
		stark.Logger.Errorf(ctx, "ImportCrowd request:%+v, err: %+v", request, err)
		return err
	}
	return nil
}

func (r *outboundTaskRepository) HandleOutboundTaskCallback(ctx context.Context, request *pb.CallbackRequest) error {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "ImportCrowd request:%+v,GetClientConn err: %+v", request, err)
		return err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	_, err = client.Callback(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ImportCrowd request:%+v, err: %+v", request, err)
		return err
	}
	return nil
}

func (r *outboundTaskRepository) GetOutboundClueList(ctx context.Context, request *pb.GetOutboundClueListRequest) (*pb.GetOutboundClueListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetOutboundClueList request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.GetOutboundClueList(ctx, request)
}

func (r *outboundTaskRepository) ExportTaskLog(ctx context.Context, request *pb.ExportTaskLogRequest) (*pb.ExportTaskLogResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetOutboundClueList request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.ExportTaskLog(ctx, request)
}

func (r *outboundTaskRepository) GetOutboundClueDetail(ctx context.Context, request *pb.GetOutboundClueDetailRequest) (*pb.GetOutboundClueDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetOutboundClueDetail request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.GetOutboundClueDetail(ctx, request)
}

func (r *outboundTaskRepository) SetTaskLogRead(ctx context.Context, request *pb.SetTaskLogReadRequest) (*pb.SetTaskLogReadResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "SetTaskLogRead request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.SetTaskLogRead(ctx, request)
}

func (r *outboundTaskRepository) CreateTask(ctx context.Context, request *pb.CreateTaskRequest) (*pb.CreateTaskResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "CreateTask request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.CreateTask(ctx, request)
}

func (r *outboundTaskRepository) UpdateTask(ctx context.Context, request *pb.UpdateTaskRequest) (*pb.UpdateTaskResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "UpdateTask request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.UpdateTask(ctx, request)
}

func (r *outboundTaskRepository) UpdateIntention(ctx context.Context, request *pb.UpdateIntentionRequest) (*pb.UpdateIntentionResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "UpdateIntention request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.UpdateIntention(ctx, request)
}

func (r *outboundTaskRepository) DeleteTask(ctx context.Context, request *pb.DeleteTaskRequest) (*pb.DeleteTaskResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "DeleteTask request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.DeleteTask(ctx, request)
}

func (r *outboundTaskRepository) ExecuteTask(ctx context.Context, request *pb.ExecuteTaskRequest) (*pb.ExecuteTaskResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "ExecuteTask request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.ExecuteTask(ctx, request)
}

// GetTaskList 获取任务列表
func (r *outboundTaskRepository) GetTaskList(ctx context.Context, request *pb.GetTaskListRequest) (*pb.GetTaskListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetTaskList request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.GetTaskList(ctx, request)
}

// GetTaskDetail 获取任务详情
func (r *outboundTaskRepository) GetTaskDetail(ctx context.Context, request *pb.GetTaskDetailRequest) (*pb.GetTaskDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetTaskDetail request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.GetTaskDetail(ctx, request)
}

// GetUnStartCallList 获取任务详情未完成的列表
func (r *outboundTaskRepository) GetUnStartCallList(ctx context.Context, request *pb.GetUnStartCallListRequest) (*pb.GetUnStartCallListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetUnStartCallList request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.GetUnStartCallList(ctx, request)
}

func (r *outboundTaskRepository) AuditOutboundClue(ctx context.Context, request *pb.AuditOutboundClueRequest) (*pb.AuditOutboundClueResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetUnStartCallList request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundTaskServiceClient(conn)
	return client.AuditOutboundClue(ctx, request)
}
